﻿#include "MatrixTest.h"
#include "ui_MatrixTest.h"
#include <QMatrix>
#include <QDebug>
#include <QDateTime>
#include <QPainterPath>

MatrixTest::MatrixTest(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MatrixTest)
{
    ui->setupUi(this);
    this->setFixedSize(400, 300);

    m_srcImage.load(":/Image/res/demo.png");
    this->setFixedSize(m_srcImage.size());
    m_drawImage = m_srcImage.scaled(this->size(), Qt::KeepAspectRatio/*, Qt::SmoothTransformation*/);
    m_rotationImage = m_drawImage;
    m_drawRect = m_drawImage.rect();

    m_selectRect = QRect(0,0,200,200);
    m_selectRect.moveCenter(this->rect().center());
}

MatrixTest::~MatrixTest()
{
    delete ui;
}

void MatrixTest::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing);
    drawImage(painter);
    drawSelectRect(painter);
}

void MatrixTest::drawImage(QPainter &painter)
{
    painter.save();
    painter.drawImage(m_drawRect, m_drawImage);
    painter.restore();
}

void MatrixTest::drawSelectRect(QPainter &painter)
{
    QPainterPath path;
    path.addRect(this->rect());
    path.addRect(m_selectRect);

    painter.save();
    painter.setBrush(QColor("#A0000000"));
    painter.drawPath(path);
    painter.restore();
}

void MatrixTest::roationImage(const bool &bFlag)
{
    m_iRotationAngle = (m_iRotationAngle + 90 * (bFlag ? 1 : -1)) % 360;
    m_matrix.reset();
    m_matrix.rotate(m_iRotationAngle);
    m_rotationImage = m_srcImage.transformed(m_matrix);
    m_drawImage = m_rotationImage.scaled(m_drawRect.height(), m_drawRect.width(), Qt::KeepAspectRatio/*, Qt::SmoothTransformation*/);

    QMatrix matrix;
    matrix.rotate(90 * (bFlag ? 1 : -1));
    QPoint newPoint = matrix.map(m_drawRect.topLeft() - this->rect().center());

    QSize size = m_drawRect.size();
    m_drawRect.setWidth(size.height());
    m_drawRect.setHeight(size.width());
    if(bFlag)
    {
        m_drawRect.moveTopRight(newPoint +  this->rect().center());
    }
    else
    {
        m_drawRect.moveBottomLeft(newPoint + this->rect().center());
    }

    this->update();
}

void MatrixTest::on_pushButton_2_clicked()
{
    roationImage(true);
}
void MatrixTest::on_pushButton_3_clicked()
{
    roationImage(false);
}

void MatrixTest::wheelEvent(QWheelEvent *event)
{
    QWidget::wheelEvent(event);
    if(event->delta() > 0)
    {
        m_fZoom += 0.1f;
        if(m_fZoom > 2.0)
        {
            m_fZoom = 2.0f;
        }
    }
    else
    {
        m_fZoom -= 0.1f;
        if(m_fZoom < 1.0)
        {
            m_fZoom = 1.0;
        }
    }

    float fx = (m_selectRect.center().x() - m_drawRect.left()) * 1.f / m_drawRect.width();
    float fy = (m_selectRect.center().y() - m_drawRect.top()) * 1.0f / m_drawRect.height();

    m_drawRect = m_rotationImage.rect();
    m_drawRect.setWidth(m_drawRect.width() * m_fZoom);
    m_drawRect.setHeight(m_drawRect.height() * m_fZoom);

    float fx1 = m_selectRect.center().x() - fx * m_drawRect.width();
    float fy1 = m_selectRect.center().y() - fy * m_drawRect.height();

    m_drawRect.moveTopLeft(QPointF(fx1, fy1).toPoint());
    m_drawImage = m_rotationImage.scaled(m_drawRect.size(), Qt::KeepAspectRatio/*, Qt::SmoothTransformation*/);
    this->update();
}

void MatrixTest::mousePressEvent(QMouseEvent *event)
{
    QWidget::mousePressEvent(event);
    if(event->button() == Qt::LeftButton)
    {
        m_bMousePress = true;
        m_mousePress = event->pos() + m_offset;
    }
}

void MatrixTest::mouseMoveEvent(QMouseEvent *event)
{
    QWidget::mouseMoveEvent(event);
    if(event->buttons() & Qt::LeftButton && m_bMousePress)
    {
        m_offset = event->pos() - m_mousePress;
        QRect newRect = m_drawRect;
        newRect.moveTopLeft(m_drawRect.topLeft() + m_offset);
        if(newRect.contains(m_selectRect))
        {
            m_drawRect = newRect;
        }
        m_mousePress = event->pos();
        this->update();
    }
}

void MatrixTest::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_bMousePress = false;
    }
}

void MatrixTest::on_pushButton_clicked()
{
    m_matrix.reset();
    m_matrix.rotate(m_iRotationAngle);
    m_rotationImage = m_srcImage.transformed(m_matrix, Qt::SmoothTransformation);
    m_drawImage = m_rotationImage.scaled(m_drawRect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);

    QPixmap pix(200,200);
    pix.fill(QColor(Qt::transparent));
    QPainter painter(&pix);
    QRect saveRect = m_selectRect;
    saveRect.moveTopLeft(saveRect.topLeft() - m_drawRect.topLeft());

    painter.drawImage(pix.rect(), m_drawImage, saveRect);
    pix.save(QDateTime::currentDateTime().toString("hhmmss") + ".png", "png");
}


